package com.mg.framework.kernel;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;

public class CancelCommandController extends Actor implements Touchable {

	private Paint paint = null;
	private Paint cmdButtonPaint = null;

	private float iiX = -1;
	private float iiY = -1;

	private float iiRadius = -1;

	private Canvas canvas = null;

	private int pointerIndex = -1;
	private int pointerId = -1;
	private int iiCurrentPointerId = -1;
	private int iiPointerIndex = -1;

	private boolean iiCmdClicked = false;

	public CancelCommandController(Screen screen, int width, int height) {
		super(screen, width, height);
		this.x = this.screen.getWidth() - 2 - width;
		this.y = this.screen.getHeight() - 2 - height;

		this.iiX = this.width / 4;
		this.iiY = this.height / 4 * 3;

		this.iiRadius = this.width / 4 - 2;

		this.paint = new Paint();
		this.paint.setAntiAlias(true);
		this.paint.setAlpha(120);

		this.cmdButtonPaint = new Paint();
		this.cmdButtonPaint.setAntiAlias(true);
		this.cmdButtonPaint.setColor(Color.WHITE);

		this.canvas = new Canvas(this.bitmap);
	}

	@Override
	public void gameUpdate() {
		if (this.iiCmdClicked) {
			this.cmdButtonPaint.setColor(Color.RED);
			((ControllerScreen) this.screen).handleCancel();
		} else {
			this.cmdButtonPaint.setColor(Color.WHITE);

		}
		this.canvas.drawCircle(iiX, iiY, iiRadius, cmdButtonPaint);
	}

	@Override
	public void screenUpdate(Canvas canvas) {
		canvas.drawBitmap(this.bitmap, this.x, this.y, paint);

	}

	@Override
	public void onTouchEvent(MotionEvent event) {
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
		case MotionEvent.ACTION_POINTER_DOWN:
			if (this.iiCurrentPointerId == -1) {
				this.pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
				this.pointerId = event.getPointerId(pointerIndex);
				this.iiCurrentPointerId = pointerId;
				this.iiPointerIndex = event
						.findPointerIndex(iiCurrentPointerId);
				iiCmdClicked = true;

			}
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_POINTER_UP:
			this.pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
			this.pointerId = event.getPointerId(pointerIndex);
			if (this.iiCurrentPointerId != -1
					&& this.iiCurrentPointerId == this.pointerId) {
				this.iiPointerIndex = -1;
				this.iiCurrentPointerId = -1;
				iiCmdClicked = false;
			}
			break;
		}
	}

	@Override
	public boolean isTouched(MotionEvent event) {

		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
		case MotionEvent.ACTION_POINTER_DOWN:
			if (iiCurrentPointerId == -1) {
				this.pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
				return isInArea(event.getX(this.pointerIndex),
						event.getY(this.pointerIndex), this.iiX, this.iiY,
						this.iiRadius);
			}
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_POINTER_UP:
			if (iiCurrentPointerId != -1) {
				this.pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
				this.pointerId = event.getPointerId(pointerIndex);
				if (this.iiCurrentPointerId == this.pointerId) {
					return true;
				}
			}
		default:
			return false;
		}
	}

	private boolean isInArea(float x, float y, float cx, float cy, float r) {
		return (Math.sqrt(Math.pow((cx - (x - this.x)), 2)
				+ Math.pow((cy - (y - this.y)), 2)) < r) ? true : false;
	}

}
